﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
using System.Text;
using MixScript.DBService;

namespace MixScript.Appl.StartUp
{
    public class cDBSetting : cDBService
    {
        private string sHost = "";
        private string sDBName = "";
        private string sDBUser = "";
        private string sDBPass = "";
        private string sProvider;
        private string sConstring;
        //private cDBServer odbserver;

        public bool SaveSetting()
        {
            try
            {
                if (TestSetting())
                {

                    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    config.AppSettings.Settings.Remove("host");
                    config.AppSettings.Settings.Add("host", sHost);
                    config.AppSettings.Settings.Remove("dbname");
                    config.AppSettings.Settings.Add("dbname", sDBName);
                    config.AppSettings.Settings.Remove("userdb");
                    config.AppSettings.Settings.Add("userdb", sDBUser);
                    config.AppSettings.Settings.Remove("passdb");
                    config.AppSettings.Settings.Add("passdb", sDBPass);
                    config.Save(ConfigurationSaveMode.Modified);
                    ConfigurationManager.RefreshSection("appSettings");
                }
                return true;


            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }


        }

        public bool TestSetting()
        {
            try
            {
                genConstring();
                Dbserver.tesConn(sConstring);
                return true;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }

        }

        public void genConstring()
        {
            if (sProvider.ToLower() == "sqlserver")
            {
                sConstring = "Data Source=" + sHost + ";Initial Catalog=" + sDBName + ";user=" + sDBUser + ";password=" + sDBPass + ";Integrated Security=True";

            }
            else if (sProvider.ToLower() == "mysql")
            {
                sConstring = "Data Source=" + sHost + ";Initial Catalog=" + sDBName + ";user=" + sDBUser + ";password=" + sDBPass + ";";
            }
            else
            {
                throw new Exception("Invalid DB Provider");
            }
        }

        public bool isvalid()
        {
            if (sHost == string.Empty ||
                sDBName == string.Empty ||
                sDBUser == string.Empty)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public void updateConfig()
        {
            try
            {
                if (TestSetting())
                {

                    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    config.AppSettings.Settings.Remove("host");
                    config.AppSettings.Settings.Add("host", sHost);
                    config.AppSettings.Settings.Remove("dbname");
                    config.AppSettings.Settings.Add("dbname", sDBName);
                    config.AppSettings.Settings.Remove("userdb");
                    config.AppSettings.Settings.Add("userdb", sDBUser);
                    config.AppSettings.Settings.Remove("passdb");
                    config.AppSettings.Settings.Add("passdb", sDBPass);
                    config.Save(ConfigurationSaveMode.Modified);
                    ConfigurationManager.RefreshSection("appSettings");
                }
                //  return true;


            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }

        //public void clearField(Control[] ct)
        //{
        //    try
        //    {
        //        //  cCommon oComm = new cCommon();
        //        // oComm.clearControl(ct);
        //    }
        //    catch (Exception ex)
        //    {

        //        throw new Exception(ex.Message);
        //    }
        //}

        public void loadDBSetting()
        {
            try
            {
                //cBaseConfig cfg = new cBaseConfig();

                //tHost.Text = cfg.DBHost;
                //tDBname.Text = cfg.Dbname;
                //tUser.Text = cfg.Userdb;
                //tPass.Text = cfg.Passdb;


            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }

        public cDBSetting()
        {

        }

        public cDBSetting(string shost, string sdbname, string sdbuser, string sdbpass, string sprovider)
        {
            try
            {

                sHost = shost;
                sDBName = sdbname;
                sDBUser = sdbuser;
                sDBPass = sdbpass;
                sProvider = sprovider;
                if (isvalid())
                {
                    //  odbserver = new cDBServer(sprovider);
                    genConstring();
                }
                else
                {
                    throw new Exception("Invalid DBSetting");
                }

            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }

        }
    }
}
